﻿/***************************************************************
 * 开发人员：网魂小兵@http://www.mozlite.com
 * 
 * 目标标本：4.0.30319.239
 * 
 * 最后修改：2011/11/14 19:58:16
 * ************************************************************/
namespace Mozlite.Common
{
    using System;
    using System.Linq;
    using System.Text;
    using System.Collections.Generic;
    using Mozlite.Common.Configuration;

    /// <summary>
    /// 主题。
    /// </summary>
    public class Thread : BaseObject
    {
        /// <summary>
        /// 初始化类<see cref="T:Mozlite.Components.Thread"/>。
        /// </summary>
        public Thread()
        {
            this.SettingsID = SiteSettings.Current.SettingsID;
            this.UserID = AppContext.Current.User.UserID;
            this.UserIP = AppContext.Current.Context.Request.UserHostAddress;
            this.CreateDate = DateTime.Now;
            this.ThreadMode = Common.ThreadMode.Archive;
        }

        /// <summary>
        /// 从数据库中加载数据。
        /// </summary>
        /// <param name="reader">数据库读取器。</param>
        protected override void Load(System.Data.IDataReader reader)
        {
            this.SettingsID = reader.GetInt32("SettingsID");
            this.ThreadID = reader.GetInt32("ThreadID");
            this.CategoryID = reader.GetInt32("CategoryID");
            this.Title = reader.GetString("Title");
            this.Summary = reader.GetString("Summary");
            this.Body = reader.GetString("Body");
            this.Key = reader.GetString("Key");
            this.CreateDate = reader.GetDateTime("CreateDate");
            this.LastModify = reader.GetDateTime("LastModify");
            this.UserID = reader.GetInt32("UserID");
            this.ModeratorID = reader.GetInt32("ModeratorID");
            this.LastModeratorDate = reader.GetDateTime("LastModeratorDate");
            this.LastPostID = reader.GetInt32("LastPostID");
            this.LastPostUserID = reader.GetInt32("LastPostUserID");
            this.LastPostDate = reader.GetDateTime("LastPostDate");
            this.IsApproved = reader.GetBoolean("IsApproved");
            this.Views = reader.GetInt32("Views");
            this.AllowedApply = reader.GetBoolean("AllowedApply");
            this.AllowedAnonymousApply = reader.GetBoolean("AllowedAnonymousApply");
            this.UserIP = reader.GetString("UserIP");
            this.LogoUrl = reader.GetString("LogoUrl");
            this.IsStrict = reader.GetBoolean("IsStrict");
            this.SortOrder = reader.GetInt64("SortOrder");
            this.Tags = reader.GetString("Tags");
            this.PostCount = reader.GetInt32("PostCount");
            this.ThreadMode = reader.TGet<ThreadMode>("ThreadMode", Common.ThreadMode.Archive);
            this.ForumID = reader.TGet<int>("ForumID");
            if (string.IsNullOrEmpty(this.LogoUrl)&&this.Category != null)
                this.LogoUrl = this.Category.LogoUrl;
        }

        /// <summary>
        /// 论坛ID。
        /// </summary>
        public int ForumID { get; set; }

        /// <summary>
        /// 主题模式。
        /// </summary>
        public ThreadMode ThreadMode { get; set; }

        /// <summary>
        /// 网站设置ID。
        /// </summary>
        public int SettingsID { get; set; }
        /// <summary>
        /// 主题ID。
        /// </summary>
        public int ThreadID { get; set; }
        /// <summary>
        /// 分类ID。
        /// </summary>
        public int CategoryID { get; set; }
        /// <summary>
        /// 标题。
        /// </summary>
        public string Title { get; set; }
        /// <summary>
        /// 摘要。
        /// </summary>
        public string Summary { get; set; }
        /// <summary>
        /// 内容。
        /// </summary>
        public string Body { get; set; }
        /// <summary>
        /// 编程键。
        /// </summary>
        public string Key { get; set; }
        /// <summary>
        /// 创建日期。
        /// </summary>
        public DateTime CreateDate { get; set; }
        /// <summary>
        /// 更新日期。
        /// </summary>
        public DateTime LastModify { get; set; }
        /// <summary>
        /// 用户ID。
        /// </summary>
        public int UserID { get; set; }
        /// <summary>
        /// 操作用户ID。
        /// </summary>
        public int ModeratorID { get; set; }
        /// <summary>
        /// 最后操作日期。
        /// </summary>
        public DateTime LastModeratorDate { get; set; }
        /// <summary>
        /// 最后回复ID。
        /// </summary>
        public int LastPostID { get; set; }
        /// <summary>
        /// 最后回复用户ID。
        /// </summary>
        public int LastPostUserID { get; set; }
        /// <summary>
        /// 最后回复时间。
        /// </summary>
        public DateTime LastPostDate { get; set; }
        /// <summary>
        /// 是否通过验证。
        /// </summary>
        public bool IsApproved { get; set; }
        /// <summary>
        /// 访问次数。
        /// </summary>
        public int Views { get; set; }
        /// <summary>
        /// 允许回复。
        /// </summary>
        public bool AllowedApply { get; set; }
        /// <summary>
        /// 允许匿名回复。
        /// </summary>
        public bool AllowedAnonymousApply { get; set; }
        /// <summary>
        /// 用户IP。
        /// </summary>
        public string UserIP { get; set; }
        /// <summary>
        /// 主题Logo图标。
        /// </summary>
        public string LogoUrl { get; set; }
        /// <summary>
        /// 是否精华。
        /// </summary>
        public bool IsStrict { get; set; }
        /// <summary>
        /// 排序列，主要用于置顶。
        /// </summary>
        public long SortOrder { get; set; }
        /// <summary>
        /// 获取或设置标签。
        /// </summary>
        public string Tags { get; set; }

        private Category category;
        /// <summary>
        /// 获取当前分类实例对象。
        /// </summary>
        public Category Category {
            get {
                if (category == null)
                {
                    Categories.LoadCategories().TryGetValue(CategoryID, out category);
                }
                return category;
            }
        }

        /// <summary>
        /// 获取或设置帖子数量。
        /// </summary>
        public int PostCount { get; set; }

        private string threadUrl;
        /// <summary>
        /// 获取文章访问地址。
        /// </summary>
        public string ThreadUrl
        {
            get
            {
                if (threadUrl == null)
                {
                    if (string.IsNullOrEmpty(Key))
                        threadUrl = Mozlite.Web.UI.RouteManager.FormatUrl(this.Category.ApplicationType + "_threadviewbyid", new { id = this.ThreadID });
                    else
                        threadUrl = Mozlite.Web.UI.RouteManager.FormatUrl(this.Category.ApplicationType + "_threadviewbykey", new { key = this.Key });
                }
                return threadUrl;
            }
        }
    }
}